var moment = require('moment');
var CFG = require('../config/WXConfig');
var request = require('superagent');
var xml2js = require('xml2js');

/**
 *
 * 微信扫码支付API列表的封装
 params = {
 access_token:'xxx',
 openid :'xxxxxx'
  ...//不需要refreshtoken
 }
 */

module.exports = {

    //关闭订单
    closeOrder: function (xmlData, Callback) {
        request.post(CFG.ORDER_CLOSE_URL)
            .set('Content-Type', 'application/xml;charset=utf-8')
            .send(xmlData)
            .end(function (error, httpResponse) {
                if (httpResponse.ok) {
                    var parser = new xml2js.Parser({explicitArray: false});
                    parser.parseString(httpResponse.text, function (error, result) {
                        Callback(result, null);
                    });
                }
                else {
                    Callback(null, "网络请求异常");
                }
            });
    },
    //获取订单信息
    queryOrder: function (xmlData, Callback) {
    request.post(CFG.ORDER_QUERY_URL)
        .set('Content-Type', 'application/xml;charset=utf-8')
        .send(xmlData)
        .end(function (error, httpResponse) {
            if (httpResponse.ok) {
                var parser = new xml2js.Parser({explicitArray: false});
                parser.parseString(httpResponse.text, function (error, result) {
                    Callback(result, null);
                });
            }
            else {
                Callback(null, "网络请求异常");
            }
        });
    },

    unifiedOrder: function (xmlData, Callback) {
        request.post(CFG.UNIFIED_ORDER_URL)
            .set('Content-Type', 'application/xml;charset=utf-8')
            .send(xmlData)
            .end(function (error, httpResponse) {
                if (httpResponse.ok) {
                    var parser = new xml2js.Parser({explicitArray: false});
                    parser.parseString(httpResponse.text, function (error, result) {
                        Callback(result, null);
                    });
                }
                else {
                    Callback(null, "网络请求异常");
                }
            });
    }

}

